
# set working directory to location of the R file
#setwd("replication/scripts")

#install starpolishr for putting different stargazer graphs together as panels in same table

require(devtools)
devtools::install_github("ChandlerLutz/starpolishr")
library(starpolishr)

require(pacman)
pacman::p_load(tidyverse, lfe, dplyr, ggplot2, ggpubr, stargazer,
               purrr, cowplot, starpolishr, here, reshape)




# All models run using PanelMatch version 1.0.0

devtools::install_version("PanelMatch", version = "1.0.0", repos = "http://cran.us.r-project.org")

library(PanelMatch)

# combined panel
#load("C:/Users/bcke.egb/Dropbox (CBS)/Trump_and_the_Beachheads/Data/combined_panel.RData")
load("../Data/combined_panel.RData")

combined_panel <- as.data.frame(combined_panel)
combined_panel$period <- as.integer(combined_panel$period)
combined_panel$log_rev <- log(combined_panel$revenue)

# check staff and bureau covariates are the same
combined_panel$log_contracts <- log(combined_panel$total_bur_filings)
combined_panel$log_lobby <- log(combined_panel$total_bur_lobbyists)

# create treatment indicator
combined_panel$treat <- ifelse(combined_panel$prev_exp == 1 | combined_panel$staff_exp == 1, 1, 0)
combined_panel$treat <- ifelse(is.na(combined_panel$treat)==T,0,combined_panel$treat)

# bureaucrat and staff separate panels
load("../Data/firm_experience_panels.RData")

###
# bureaucrats
prev_exp_firm <- as.data.frame(prev_exp_firm)
prev_exp_firm$period <- as.integer(prev_exp_firm$period)
prev_exp_firm$log_rev <- log(prev_exp_firm$revenue)

# check staff and bureau covariates are the same
prev_exp_firm$log_contracts <- log(prev_exp_firm$total_filings)
prev_exp_firm$log_lobby <- log(prev_exp_firm$total_lobbyists)

# create treatment indicator
prev_exp_firm$prev_exp2 <- ifelse(is.na(prev_exp_firm$prev_exp)==T,0,prev_exp_firm$prev_exp)

###
# staff
staff_exp_firm <- as.data.frame(staff_exp_firm)
staff_exp_firm$period <- as.integer(staff_exp_firm$period)
staff_exp_firm$log_rev <- log(staff_exp_firm$revenue)

# check staff and bureau covariates are the same
staff_exp_firm$log_contracts <- log(staff_exp_firm$total_filings)
staff_exp_firm$log_lobby <- log(staff_exp_firm$total_lobbyists)

# create treatment indicator
staff_exp_firm$staff_exp2 <- ifelse(is.na(staff_exp_firm$staff_exp)==T,0,staff_exp_firm$staff_exp)


#---------
# firm longevity

long <- combined_panel %>%
  group_by(reg_lobby_core_id) %>%
  summarise(long = n()/2)

mean(long$long)

long_treat <- combined_panel %>%
  group_by(reg_lobby_core_id) %>%
  mutate(any_treat = mean(treat, na.rm=T),
         any_treat = ifelse(any_treat > 0, 1, 0)) %>%
  group_by(reg_lobby_core_id, any_treat) %>%
  summarise(long = n()/2)

long_treat %>%
  group_by(any_treat) %>%
  summarise(mean(long))

#-----
# lobbyists entering politics by year

prev_exp_firm <- prev_exp_firm %>%
  filter(prev_exp2==1) %>%
  group_by(reg_lobby_core_id) %>%
  summarise(first_treat = min(period, na.rm=T))%>%
  right_join(prev_exp_firm, by = "reg_lobby_core_id")

n_bureau <- prev_exp_firm %>%
  filter(duplicated(reg_lobby_core_id) == FALSE) %>%
  group_by(first_treat) %>%
  summarise(n_lobby = n()) %>%
  drop_na(first_treat)
n_bureau$type <- "Bureaucrat"

staff_exp_firm <- staff_exp_firm %>%
  filter(staff_exp2==1) %>%
  group_by(reg_lobby_core_id) %>%
  summarise(first_treat = min(period, na.rm=T))%>%
  right_join(staff_exp_firm, by = "reg_lobby_core_id")

n_staff <- staff_exp_firm %>%
  filter(duplicated(reg_lobby_core_id) == FALSE) %>%
  group_by(first_treat) %>%
  summarise(n_lobby = n()) %>%
  drop_na(first_treat)

n_staff$type <- "Staff"

n_lobby <- rbind(n_bureau, n_staff)

#############
# convenience functions

# function for extracting id of control firms

get_unique_control <- function(event_id, pm_obj){
  
  crnt_id <- filter(overview, id == event_id)
  
  test <- pm_obj$att
  
  test2 <- as.numeric(as.character(test[[event_id]]))
  
  test3 <- filter(combined_panel, reg_lobby_core_id %in% test2 ) %>%
    filter(!duplicated(reg_lobby_core_id)) %>%
    select(reg_lobby_core_id)
  
  return(test3)
  
  
}  

# wrapper function for PanelMatch

pm_wrap <- function(lags, dataset, treat, refine = F,
                    DV, seed){
  
  if(refine == F){
  comb <- PanelMatch(lag = lags, 
                     time.id = "period", unit.id = "reg_lobby_core_id", 
                     treatment = treat, 
                     refinement.method = "none", 
                     data = dataset, 
                     match.missing = T, 
                     qoi = "att" ,
                     outcome.var = DV,
                     lead = 0:4, 
                     forbid.treatment.reversal = T)
  
  dist_control <- comb$att
  comb_obs <- sum(print(comb$att)[,3])
  
  if(nchar(comb_obs) == 6){
  comb_obs <- paste(substr(comb_obs, 1, 3), ",", 
        substr(comb_obs, 4, nchar(comb_obs)), sep = "")}else{
          comb_obs <- paste(substr(comb_obs, 1, 2), ",", 
                            substr(comb_obs, 3, nchar(comb_obs)), sep = "")
        }
  
  unique_treat <- length(unique(print(comb$att)[,1]))
  
  # get unique control firms
  overview <- print(comb$att)
  overview[,2] <- as.numeric(as.character(overview[,2]))
  overview$id <- 1:nrow(overview)
  
  
  unique_control <- map_df(overview$id,
                           ~ get_unique_control(., pm_obj = comb))
  
  unique_control <- length(unique(unique_control$reg_lobby_core_id))
  
  
  unique_control_txt <- paste(substr(unique_control, 1, 1), ",", 
                          substr(unique_control, 2, nchar(unique_control)),
                          sep = "")
  
  }else{comb <- PanelMatch(lag = lags, 
                           time.id = "period", unit.id = "reg_lobby_core_id", 
                           treatment = treat, 
                           refinement.method = "mahalanobis", #alternative none
                           data = dataset, 
                           match.missing = T, 
                           covs.formula = ~ I(lag(log_contracts,1:4)) + I(lag(log_lobby,1:4)) + I(lag(log_rev,1:4)), 
                           qoi = "att" ,
                           outcome.var = DV,
                           lead = 0:4, 
                           forbid.treatment.reversal = T)
  dist_control <- comb$att
  comb_obs <- sum(print(comb$att)[,3])
  
  if(nchar(comb_obs) == 6){
    comb_obs <- paste(substr(comb_obs, 1, 3), ",", 
                      substr(comb_obs, 4, nchar(comb_obs)), sep = "")}else{
                        comb_obs <- paste(substr(comb_obs, 1, 2), ",", 
                                          substr(comb_obs, 3, nchar(comb_obs)), sep = "")
                      }
  
  unique_treat <- length(unique(print(comb$att)[,1]))
  
  # get unique control firms
  overview <- print(comb$att)
  overview[,2] <- as.numeric(as.character(overview[,2]))
  overview$id <- 1:nrow(overview)
  
  unique_control <- map_df(overview$id,
                           ~ get_unique_control(., pm_obj = comb))
  
  unique_control <- length(unique(unique_control$reg_lobby_core_id))
  
  unique_control_txt <- paste(substr(unique_control, 1, 1), ",", 
                          substr(unique_control, 2, nchar(unique_control)),
                          sep = "")
  
  }
  
  set.seed(seed)
  comb_results <- PanelEstimate(sets = comb, 
                                data = dataset, 
                                confidence.level = .9)
  
  pm_res <- as.data.frame(summary(comb_results)[[1]][1:5, 1:2])
  
  
  return(list(res = pm_res, 
              n_control = comb_obs, 
              n_treat = unique_treat,
              n_unique_control = unique_control,
              unique_control_txt = unique_control_txt,
              dist_control = dist_control))
  
}


## second wrapper function for unified/divided results 




##########
# illustrate Imai-Kim-Wang methodology

# Make a matched set for illustration
comb <- PanelMatch(lag = 6, 
                     time.id = "period", unit.id = "reg_lobby_core_id", 
                     treatment = "treat", 
                     refinement.method = "none", 
                     data = combined_panel, 
                     match.missing = T, 
                     qoi = "att" ,
                     outcome.var = "log_rev",
                     lead = 0:4, 
                     forbid.treatment.reversal = T)
comb
comb_obs <- sum(print(comb$att)[,3])

# show treatment for subset of firms
overview <- print(comb$att)
overview[,2] <- as.numeric(as.character(overview[,2]))
overview$id <- 1:nrow(overview)

test <- comb$att

test2 <- as.numeric(as.character(test[[6]]))

#test2 <- as.numeric(as.character(test[[38]]))

test3 <- filter(combined_panel, #period %in% (overview[1,2]-6):(overview[1,2]+4) &
                  reg_lobby_core_id %in% test2 | reg_lobby_core_id == overview[1,1])

sub_match <- PanelMatch(lag = 6, 
                   time.id = "period", unit.id = "reg_lobby_core_id", 
                   treatment = "treat", 
                   refinement.method = "none", 
                   data = test3, 
                   match.missing = T, 
                   qoi = "att" ,
                   outcome.var = "log_rev",
                   lead = 0:4, 
                   forbid.treatment.reversal = T)

DisplayTreatment(unit.id = "reg_lobby_core_id",
                 time.id = "period", legend.position = "none",
                 xlab = "Reporting Period", ylab = "Firm",
                 treatment = "treat", data = test3, 
                 dense.plot = T,
                 hide.x.axis.label = TRUE, 
                 hide.y.axis.label = TRUE)

mset<- sub_match$att

p_treat <- DisplayTreatment(unit.id = "reg_lobby_core_id",
                 time.id = "period", legend.position = "none",
                 xlab = "Time", ylab = "Firms",
                 treatment = "treat", data = test3, 
                 dense.plot = F,
                 matched.set = mset[1],
                 hide.y.axis.label = TRUE, title = NULL,
                 show.set.only = F, hide.x.axis.label = TRUE) + ggtitle(NULL)


#,color.of.treated = "black", color.of.untreated = "grey"

p_treat

ggsave(p_treat, filename = "../images/Figure3.pdf",
       width = 7, height = 5)

#---------------------------------------------------
# Details on the DiD estimation

# Distribution of counts of control group firms

cont <- lapply(comb$att, length)
cont <- do.call("rbind", cont)

p1 <- cont %>% as.data.frame() %>%
ggplot(aes(x = V1)) +
  geom_histogram(bins = 50) +
  theme_classic() +
  labs(x = "Number of Unique Firms\nin Each Control Group",
       y = "Count")

ggsave(p1, filename = "../images/FigureC6.pdf",
         width = 6.5, height = 5.5)

median(cont)

#####
#Balance

comb <- PanelMatch(lag = 6, 
                   time.id = "period", unit.id = "reg_lobby_core_id", 
                   treatment = "treat", 
                   refinement.method = "mahalanobis", 
                   #refinement.method = "none", 
                   
                   data = combined_panel, 
                   match.missing = T, 
                   qoi = "att" ,
                   outcome.var = "log_rev",
                   covs.formula = ~ I(lag(log_contracts,1:6)) + I(lag(log_lobby,1:6)) + I(lag(log_rev,1:6)), 
                   lead = 0:4, 
                   forbid.treatment.reversal = T)

rev_trend <- data.frame(est = get_covariate_balance(comb$att,
                                         data = combined_panel,
                                         covariates = c("log_rev"),
                                         plot = F,
                                         use.equal.weights = F),
                        time = -6:0)


ggplot(rev_trend, aes(x = time, y = log_rev)) +
  geom_line() +
  geom_point(size = 1) +
  theme_classic() +
  scale_y_continuous(limits = c(0,0.6)) +
  scale_x_continuous(breaks = -6:0,
                     labels = as.character(c(-6:-1,
                                             "0\n(Lobbyist\nHired)"))) +
  labs(x = "Semesters Until Treatment",
       y = "Revenue Gap Between Treatment\nand Control Firms")

ggsave(filename = "../images/Figure4.pdf",
         width = 8,
         height = 4 )


pre_trend <- rbind(data.frame(est = melt(get_covariate_balance(comb$att,
                      data = combined_panel,
                      covariates = c("log_contracts", "log_lobby","log_rev"),
                      plot = F,
                      use.equal.weights = T)),
                      time = -6:0,
           adj = "Unadjusted"),
          data.frame(est = melt(get_covariate_balance(comb$att,
                                           data = combined_panel,
                                           covariates = c("log_contracts", "log_lobby","log_rev"),
                                           plot = F,
                                           use.equal.weights = F)),
                     time = -6:0,
                     adj= "Adjusted"))

  ggplot(pre_trend, aes(x = time, y = est.value, colour = est.Var2,
                      group = interaction(est.Var2, adj))) +
  geom_line() +
  geom_point(size = 1) +
  theme_classic() +
  scale_y_continuous(limits = c(0, 1.6)) +
  scale_x_continuous(breaks = -6:0,
                     labels = as.character(c(-6:-1,
                                           "0\n(Lobbyist\nHired)"))) +
  labs(x = "Time Until Treatment",
       y = "Difference Between Treatment\nand Control Firms") +
  scale_color_manual(name = NULL,
                     values = c("darkgrey", "black", "lightgrey"),
                     labels = c("ln Filings",
                                "ln Lobbyists",
                                "ln Revenue")) +
  annotate(geom = "text", x = -2, y = 1.25,
           label = "Unadjusted", size = 2.5) +
  annotate(geom = "text", x = -2, y = 0.45,
           label = "Adjusted", size = 2.5) 

ggsave(filename = "../images/FigureC7.pdf",
         width = 7,
         height = 5 )

###########
# estimate models

#w/o adjustment
main <- pm_wrap(lags = 6, dataset = combined_panel,
                treat = "treat", refine = F, DV = "log_rev", seed = 888)


# With adjustment
main2 <- pm_wrap(lags = 6, dataset = combined_panel,
                 treat = "treat", refine = T, DV = "log_rev", seed = 888)

############
# compile and export tables for LaTeX

# twoway FE
mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 
summary(mod)

# Panel A
pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  main$res[,1], se = main$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Lobby Firm Revenue"), 
                  label = c("mainined"),
                  add.lines = list(c("Treatment Events", main$n_treat, main$n_treat, main$n_treat,
                                     main$n_treat, main$n_treat),
                                   c("Control Firms", main$n_control, main$n_control,
                                     main$n_control, main$n_control, main$n_control),
                                   c("Unique Controls", main$n_unique_control, main$n_unique_control,
                                     main$n_unique_control, main$n_unique_control, main$n_unique_control)),
                  header = F)

#Panel B
pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  main2$res[,1], se = main2$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employees and Lobby Firm Revenue"), 
                  label = c("mainined_adj"),
                  add.lines = list(c("Treatment Events", main2$n_treat, main2$n_treat, main2$n_treat,
                                     main2$n_treat, main2$n_treat),
                                   c("Control Firms", main2$n_control, main2$n_control,
                                     main2$n_control, main2$n_control, main2$n_control),
                                   c("Unique Controls", main2$n_unique_control, main2$n_unique_control,
                                     main2$n_unique_control, main2$n_unique_control, main2$n_unique_control)),
                  header = F)

# put mainined table together
BothPanels <- star_panel(pan1, pan2, panel.names = c("Unadjusted Estimates", "Adjusted Estimates"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. In Panel B, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(BothPanels, 
               file = "../tables/Table2.tex")

#----------------------------------
# Fx driven by revenue/filing 


combined_panel$pr_filing_nl <- combined_panel$revenue / combined_panel$total_staff_filings

combined_panel$pr_filing <- log(combined_panel$pr_filing_nl)
combined_panel$log_filings <- log(combined_panel$total_staff_filings)

comb_prfiling <- pm_wrap(lags = 6, dataset = combined_panel,
                       treat = "treat", refine = F,
                       DV = "pr_filing", seed = 888)
comb_prfiling$res


comb_Nfiling <- pm_wrap(lags = 6, dataset = combined_panel,
                         treat = "treat", refine = F,
                         DV = "log_filings", seed = 888)

comb_Nfiling$res
#------------------------------
#high end: less than one additional client
mean(combined_panel$total_staff_filings)*0.05965915
#--------------------------------------------

# helper model object for stargazer
mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 

# panels for filings based outcomes
star_prfile <- stargazer(mod, mod, mod, mod, mod, 
          coef =  comb_prfiling$res[,1], se = comb_prfiling$res[,2],
          covariate.labels = c("Lobbyist Becomes Government Employee"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln(Revenue / Filing)", 
          title = c("Lobbyists Turning Government Employee and ln Revenue Per Filing"), 
          label = c("Revfiling"),
          add.lines = list(c("Treatment Events", comb_prfiling$n_treat, comb_prfiling$n_treat, comb_prfiling$n_treat,
                             comb_prfiling$n_treat, comb_prfiling$n_treat),
                           c("Control Firms", comb_prfiling$n_control, comb_prfiling$n_control,
                             comb_prfiling$n_control, comb_prfiling$n_control, comb_prfiling$n_control),
                           c("Unique Controls", comb_prfiling$n_unique_control, comb_prfiling$n_unique_control,
                             comb_prfiling$n_unique_control, comb_prfiling$n_unique_control, comb_prfiling$n_unique_control)),
          header = F)



star_file <- stargazer(mod, mod, mod, mod, mod, 
          coef =  comb_Nfiling$res[,1], se = comb_Nfiling$res[,2],
          covariate.labels = c("Lobbyist Becomes Government Employee"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Clients", 
          title = c("Lobbyists Turning Government Employee and ln Number of Filings"), 
          label = c("Nfilings"),
          add.lines = list(c("Treatment Events", comb_Nfiling$n_treat, comb_Nfiling$n_treat, comb_Nfiling$n_treat,
                             comb_Nfiling$n_treat, comb_Nfiling$n_treat),
                           c("Control Firms", comb_Nfiling$n_control, comb_Nfiling$n_control,
                             comb_Nfiling$n_control, comb_Nfiling$n_control, comb_Nfiling$n_control),
                           c("Unique Controls", comb_Nfiling$n_unique_control, comb_Nfiling$n_unique_control,
                             comb_Nfiling$n_unique_control, comb_Nfiling$n_unique_control, comb_Nfiling$n_unique_control)),
          header = F)


# total clients outcome

combined_panel$log_clients <- log(combined_panel$total_clients)

## total clients:
comb_Nclient <- pm_wrap(lags = 6, dataset = combined_panel,
                        treat = "treat", refine = F, DV = "log_clients", seed = 888)


## revenue per client
combined_panel$rev_client <- combined_panel$revenue/combined_panel$total_clients
combined_panel$log_rev_client <- log(combined_panel$rev_client)

comb_prclient <- pm_wrap(lags = 6, dataset = combined_panel,
                         treat = "treat", refine = F, 
                         DV = "log_rev_client", seed = 888)

# panels for client based outcomes
star_prclient <- stargazer(mod, mod, mod, mod, mod, 
          coef =  comb_prclient$res[,1], se = comb_prclient$res[,2],
          covariate.labels = c("Lobbyist Becomes Government Employee"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue per Client", 
          title = c("Lobbyists Turning Government Employee and Number of Clients"), 
          label = c("combined"),
          add.lines = list(c("Treatment Events", comb_prclient$n_treat, comb_prclient$n_treat, comb_prclient$n_treat,
                             comb_prclient$n_treat, comb_prclient$n_treat),
                           c("Control Firms", comb_prclient$n_control, comb_prclient$n_control,
                             comb_prclient$n_control, comb_prclient$n_control, comb_prclient$n_control),
                           c("Unique Controls", comb_prclient$n_unique_control, comb_prclient$n_unique_control,
                             comb_prclient$n_unique_control, comb_prclient$n_unique_control, comb_prclient$n_unique_control)),
          header = F)

#table 2
star_client <- stargazer(mod, mod, mod, mod, mod, 
          coef =  comb_Nclient$res[,1], se = comb_Nclient$res[,2],
          covariate.labels = c("Lobbyist Becomes Government Employee"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue per Client", 
          title = c("Lobbyists Turning Government Employee and ln Total Clients"), 
          label = c("combined_adj"),
          add.lines = list(c("Treatment Events", comb_Nclient$n_treat, comb_Nclient$n_treat, comb_Nclient$n_treat,
                             comb_Nclient$n_treat, comb_Nclient$n_treat),
                           c("Control Firms", comb_Nclient$n_control, comb_Nclient$n_control,
                             comb_Nclient$n_control, comb_Nclient$n_control, comb_Nclient$n_control),
                           c("Unique Controls", comb_Nclient$n_unique_control, comb_Nclient$n_unique_control,
                             comb_Nclient$n_unique_control, comb_Nclient$n_unique_control, comb_Nclient$n_unique_control)),
          header = F)



# put combined table together
BothPanels <- star_panel(star_client, star_prclient,
                         panel.names = c("ln Number of Clients", "ln(Revenue / Client)"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(BothPanels, 
               file = "../tables/Table3.tex")


BothPanels <- star_panel(star_file, star_prfile,
                         panel.names = c("ln Number of Filings", "ln(Revenue / Filing"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")


star_tex_write(BothPanels, 
               file = "../tables/TableD6.tex")

# add'l results -----------------------------------------------------------


# total lobbyists outcome -------------------------------------------------


## number of lobbyists:

combined_panel_change <- filter(combined_panel, !is.na(lobbyist_change))

combined_panel_change$lobbyist_change_positive <- 0
combined_panel_change$lobbyist_change_positive[combined_panel_change$lobbyist_change > 0 & !is.na(combined_panel_change$lobbyist_change)] <- 1

#w/o adjustment
ch_lobby_mod <- pm_wrap(lags = 6, dataset = combined_panel_change,
                treat = "treat", refine = F, DV = "lobbyist_change", seed = 888)


# With adjustment
ch_lobby_mod2 <- pm_wrap(lags = 6, dataset = combined_panel_change,
                 treat = "treat", refine = T, DV = "lobbyist_change", seed = 888)

############
# compile and export tables for LaTeX

# twoway FE
mod <- felm(lobbyist_change ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel_change) 

# table 1
ch_lobby_pan1 <- stargazer(mod, mod, mod, mod, mod, 
          coef =  ch_lobby_mod$res[,1], se = ch_lobby_mod$res[,2],
          covariate.labels = c("Lobbyist Becomes Government Employee"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "Change in Number of Lobbyists", 
          title = c("Lobbyists Turning Government Employee and Change in Number of Lobbyists"), 
          label = c("ch_lobby_modined"),
          add.lines = list(c("Treatment Events", ch_lobby_mod$n_treat, ch_lobby_mod$n_treat, ch_lobby_mod$n_treat,
                             ch_lobby_mod$n_treat, ch_lobby_mod$n_treat),
                           c("Control Firms", ch_lobby_mod$n_control, ch_lobby_mod$n_control,
                             ch_lobby_mod$n_control, ch_lobby_mod$n_control, ch_lobby_mod$n_control),
                           c("Unique Controls", ch_lobby_mod$n_unique_control, ch_lobby_mod$n_unique_control,
                             ch_lobby_mod$n_unique_control, ch_lobby_mod$n_unique_control, ch_lobby_mod$n_unique_control)),
          header = F)

#table 2
ch_lobby_pan2 <- stargazer(mod, mod, mod, mod, mod, 
          coef =  ch_lobby_mod2$res[,1], se = ch_lobby_mod2$res[,2],
          covariate.labels = c("Lobbyist Becomes Government Employee"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "Change in Number of Lobbyists", 
          title = c("Lobbyists Turning Government Employees and Change in Number of Lobbyists"), 
          label = c("ch_lobby_modined_adj"),
          add.lines = list(c("Treatment Events", ch_lobby_mod2$n_treat, ch_lobby_mod2$n_treat, ch_lobby_mod2$n_treat,
                             ch_lobby_mod2$n_treat, ch_lobby_mod2$n_treat),
                           c("Control Firms", ch_lobby_mod2$n_control, ch_lobby_mod2$n_control,
                             ch_lobby_mod2$n_control, ch_lobby_mod2$n_control, ch_lobby_mod2$n_control),
                           c("Unique Controls", ch_lobby_mod2$n_unique_control, ch_lobby_mod2$n_unique_control,
                             ch_lobby_mod2$n_unique_control, ch_lobby_mod2$n_unique_control, ch_lobby_mod2$n_unique_control)),
          header = F)

LobbyPanels <- star_panel(ch_lobby_pan1, ch_lobby_pan2, 
                           panel.names = c("Unadjusted Estimates", "Adjusted Estimates"))

# add a note
LobbyPanels <- star_notes_tex(LobbyPanels, note.type = "threeparttable",
                               note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. In Panel B, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(LobbyPanels, 
               file = "../tables/TableD5.tex")

#----------------------------------------
# Bureaucrats and staff results

# bureaucrats
# bureaucrats
prev_exp_firm <- as.data.frame(prev_exp_firm)
prev_exp_firm$period <- as.integer(prev_exp_firm$period)
prev_exp_firm$log_rev <- log(prev_exp_firm$revenue)

# check staff and bureau covariates are the same
prev_exp_firm$log_contracts <- log(prev_exp_firm$total_filings)
prev_exp_firm$log_lobby <- log(prev_exp_firm$total_lobbyists)

# create treatment indicator
prev_exp_firm$prev_exp2 <- ifelse(is.na(prev_exp_firm$prev_exp)==T,0,prev_exp_firm$prev_exp)

bureau_norefine <- pm_wrap(lags = 6, dataset = prev_exp_firm,
                           treat = "prev_exp2", DV = "log_rev",
                           refine = F, seed = 888)

bureau_pan1 <- stargazer(mod, mod, mod, mod, mod, 
          coef =  bureau_norefine$res[,1], se = bureau_norefine$res[,2],
          covariate.labels = c("Lobbyist turns Bureaucrat"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue", 
          title = c("Lobbyists Turning Bureaucrat and Lobby Firm Revenue"), 
          label = c("bureau"),
          add.lines = list(c("Treatment Events", bureau_norefine$n_treat, bureau_norefine$n_treat, bureau_norefine$n_treat,
                             bureau_norefine$n_treat, bureau_norefine$n_treat),
                           c("Control Firms", bureau_norefine$n_control, bureau_norefine$n_control,
                             bureau_norefine$n_control, bureau_norefine$n_control, bureau_norefine$n_control),
                           c("Unique Controls", bureau_norefine$unique_control_txt, bureau_norefine$unique_control_txt,
                             bureau_norefine$unique_control_txt, bureau_norefine$unique_control_txt, bureau_norefine$unique_control_txt)),
          header = F)


bureau_refine <- pm_wrap(lags = 6, dataset = prev_exp_firm,
                         treat = "prev_exp2", DV = "log_rev",
                         refine = T, seed = 888)

bureau_pan2 <- stargazer(mod, mod, mod, mod, mod, 
          coef =  bureau_refine$res[,1], se = bureau_refine$res[,2],
          covariate.labels = c("Lobbyist turns Bureaucrat"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue", 
          title = c("Lobbyists Turning Bureaucrat and Lobby Firm Revenue"), 
          label = c("bureau2"),
          add.lines = list(c("Treatment Events", bureau_refine$n_treat, bureau_refine$n_treat, bureau_refine$n_treat,
                             bureau_refine$n_treat, bureau_refine$n_treat),
                           c("Control Firms", bureau_refine$n_control, bureau_refine$n_control,
                             bureau_refine$n_control, bureau_refine$n_control, bureau_refine$n_control),
                           c("Unique Controls", bureau_refine$unique_control_txt, bureau_refine$unique_control_txt,
                             bureau_refine$unique_control_txt, bureau_refine$unique_control_txt, bureau_refine$unique_control_txt)),
          header = F)

# put tables together as panels
BureauPanels <- star_panel(bureau_pan1, bureau_pan2, 
                           panel.names = c("Unadjusted Estimates", "Adjusted Estimates"))

# add a note
BureauPanels <- star_notes_tex(BureauPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. In Panel B, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(BureauPanels, 
               file = "../tables/Table6.tex")


# staff
staff_exp_firm <- as.data.frame(staff_exp_firm)
staff_norefine <- pm_wrap(lags = 6, dataset = staff_exp_firm,
                          treat = "staff_exp2",  DV = "log_rev",
                          refine = F, seed = 888)

staff_pan1 <- stargazer(mod, mod, mod, mod, mod, 
          coef =  staff_norefine$res[,1], se = staff_norefine$res[,2],
          covariate.labels = c("Lobbyist turns Staffer"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue", 
          title = c("Lobbyists Turning Staffer and Lobby Firm Revenue"), 
          label = c("staff"),
          add.lines = list(c("Treatment Events", staff_norefine$n_treat, staff_norefine$n_treat, staff_norefine$n_treat,
                             staff_norefine$n_treat, staff_norefine$n_treat),
                           c("Control Firms", staff_norefine$n_control, staff_norefine$n_control,
                             staff_norefine$n_control, staff_norefine$n_control, staff_norefine$n_control),
                           c("Unique Controls", staff_norefine$unique_control_txt, staff_norefine$unique_control_txt,
                             staff_norefine$unique_control_txt, staff_norefine$unique_control_txt, staff_norefine$unique_control_txt)),
          header = F)



staff_refine <- pm_wrap(lags = 6, dataset = staff_exp_firm,
                        treat = "staff_exp2",  DV = "log_rev",
                        refine = T, seed = 888)

staff_pan2 <-stargazer(mod, mod, mod, mod, mod, 
          coef =  staff_refine$res[,1], se = staff_refine$res[,2],
          covariate.labels = c("Lobbyist turns Staffer"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue", 
          title = c("Lobbyists Turning Staffer and Lobby Firm Revenue"), 
          label = c("staff2"),
          add.lines = list(c("Treatment Events", staff_refine$n_treat, staff_refine$n_treat, staff_refine$n_treat,
                             staff_refine$n_treat, staff_refine$n_treat),
                           c("Control Firms", staff_refine$n_control, staff_refine$n_control,
                             staff_refine$n_control, staff_refine$n_control, staff_refine$n_control),
                           c("Unique Controls", staff_refine$unique_control_txt, staff_refine$unique_control_txt,
                             staff_refine$unique_control_txt, staff_refine$unique_control_txt, staff_refine$unique_control_txt)),
          header = F)

# put tables together as panels
StaffPanels <- star_panel(staff_pan1, staff_pan2, 
                           panel.names = c("Unadjusted Estimates", "Adjusted Estimates"))

# add a note
StaffPanels <- star_notes_tex(StaffPanels, note.type = "threeparttable",
                               note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. In Panel B, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(StaffPanels, 
               file = "../tables/Table5.tex")




# bureaucrats and staff and divided govt ----------------------------------
# indicator for unified / divided
year_periods <- data.frame(period = seq(1,42), year = rep(seq(2000, 2020), each=2)) %>% 
  mutate(unified = case_when(
    year < 2007 ~ 1,
    year >= 2007 & year< 2009 ~ 0,
    year >= 2009 & year < 2011 ~ 1,
    year >= 2011 & year < 2017 ~0,
    year >= 2017 & year < 2019 ~1,
    year >= 2019 ~ 1
  ))


combined_panel <- left_join(combined_panel, year_periods)

combined_panel$divided <- abs(combined_panel$unified-1)
combined_panel$divided_treat <- combined_panel$treat*combined_panel$divided
combined_panel$unified_treat <- combined_panel$treat*combined_panel$unified


#w/o adjustment
comb_unified <- pm_wrap(lags = 6, dataset = combined_panel,
                treat = "unified_treat", refine = T, DV = "log_rev", seed = 888)


# With adjustment
comb_divided <- pm_wrap(lags = 6, dataset = combined_panel,
                 treat = "divided_treat", refine = T, DV = "log_rev", seed = 888)





prev_exp_firm <- left_join(prev_exp_firm, year_periods)
prev_exp_firm$divided <- abs(prev_exp_firm$unified-1)

# code new treatments for staff / brueacrats based on divided/unified
prev_exp_firm$divided_treat <- prev_exp_firm$prev_exp2*prev_exp_firm$divided
prev_exp_firm$unified_treat <- prev_exp_firm$prev_exp2*prev_exp_firm$unified



bureau_divided <- pm_wrap(lags = 6, dataset = prev_exp_firm,
                           treat = "divided_treat", DV = "log_rev",
                           refine = T, seed = 888)

bureau_unified <- pm_wrap(lags = 6, dataset = prev_exp_firm,
                          treat = "unified_treat", DV = "log_rev",
                          refine = T, seed = 888)


staff_exp_firm <- left_join(staff_exp_firm, year_periods)

staff_exp_firm$divided <- abs(staff_exp_firm$unified-1)
staff_exp_firm$divided_treat <- staff_exp_firm$staff_exp2*staff_exp_firm$divided
staff_exp_firm$unified_treat <- staff_exp_firm$staff_exp2*staff_exp_firm$unified


staff_divided <- pm_wrap(lags = 6, dataset = staff_exp_firm,
                          treat = "divided_treat",  DV = "log_rev",
                          refine = T, seed = 888)

staff_unified <- pm_wrap(lags = 6, dataset = staff_exp_firm,
                         treat = "unified_treat",  DV = "log_rev",
                         refine = T, seed = 888)


mod <- felm(lobbyist_change ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 

pan1 <- stargazer(mod, mod, mod, mod, mod, 
                        coef =  comb_unified$res[,1], se = comb_unified$res[,2],
                        covariate.labels = c("Lobbyist Revolves (Unified Govt.)"),
                        column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                        omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                        dep.var.labels = "ln Revenue", 
                        title = c("Lobbying Revenue and Unified versus Divided Government"), 
                        font.size = "scriptsize",
                        label = c("unified_divided"),
                        add.lines = list(c("Treatment Events - Unified (Full Sample)", comb_unified$n_treat, comb_unified$n_treat, comb_unified$n_treat,
                                           comb_unified$n_treat, comb_unified$n_treat),
                                         c("Treatment Events - Divided (Full Sample)", comb_divided$n_treat, comb_divided$n_treat, comb_divided$n_treat,
                                           comb_divided$n_treat, comb_divided$n_treat),
                                         c("Treatment Events - Unified (Bureaucrats)", bureau_unified$n_treat, bureau_unified$n_treat, bureau_unified$n_treat,
                                           bureau_unified$n_treat, bureau_unified$n_treat),
                                         c("Treatment Events - Divided (Bureaucrats)", bureau_divided$n_treat, bureau_divided$n_treat, bureau_divided$n_treat,
                                           bureau_divided$n_treat, bureau_divided$n_treat),
                                         c("Treatment Events - Unified (Staff)", staff_unified$n_treat, staff_unified$n_treat, staff_unified$n_treat,
                                           staff_unified$n_treat, staff_unified$n_treat), 
                                         c("Treatment Events - Divided (Staff)", staff_divided$n_treat, staff_divided$n_treat, staff_divided$n_treat,
                                           staff_divided$n_treat, staff_divided$n_treat)),
                        header = F)

pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb_divided$res[,1], se = comb_divided$res[,2],
                  covariate.labels = c("Lobbyist Revolves (Divided Govt.)"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbying Revenue and Unified versus Divided Government"), 
                  #label = c("staff"),
                  # add.lines = list(c("Treatment Events (full sample)", comb_unified$n_treat, comb_unified$n_treat, comb_unified$n_treat,
                  #                    comb_unified$n_treat, comb_unified$n_treat)),
                  header = F)


pan3 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  bureau_unified$res[,1], se = bureau_unified$res[,2],
                  covariate.labels = c("Lobbyist becomes Bureaucrat (Unified Govt.)"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbying Revenue and Unified versus Divided Government"), 
                  #label = c("staff"),
                  # add.lines = list(c("Treatment Events (full sample)", comb_unified$n_treat, comb_unified$n_treat, comb_unified$n_treat,
                  #                    comb_unified$n_treat, comb_unified$n_treat)),
                  header = F)

pan4 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  bureau_divided$res[,1], se = bureau_divided$res[,2],
                  covariate.labels = c("Lobbyist becomes Bureaucrat (Divided Govt.)"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbying Revenue and Unified versus Divided Government"), 
                  #label = c("staff"),
                  # add.lines = list(c("Treatment Events (full sample)", comb_unified$n_treat, comb_unified$n_treat, comb_unified$n_treat,
                  #                    comb_unified$n_treat, comb_unified$n_treat)),
                  header = F)

pan5 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  staff_unified$res[,1], se = staff_unified$res[,2],
                  covariate.labels = c("Lobbyist becomes Staffer (Unified Govt.)"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbying Revenue and Unified versus Divided Government"), 
                  #label = c("staff"),
                  # add.lines = list(c("Treatment Events (full sample)", comb_unified$n_treat, comb_unified$n_treat, comb_unified$n_treat,
                  #                    comb_unified$n_treat, comb_unified$n_treat)),
                  header = F)

pan6 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  staff_divided$res[,1], se = staff_divided$res[,2],
                  covariate.labels = c("Lobbyist becomes Staffer (Divided Govt.)"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbying Revenue and Unified versus Divided Government"), 
                  #label = c("staff"),
                  # add.lines = list(c("Treatment Events (full sample)", comb_unified$n_treat, comb_unified$n_treat, comb_unified$n_treat,
                  #                    comb_unified$n_treat, comb_unified$n_treat)),
                  header = F)


StaffPanels <- star_panel(pan1, pan2, pan3, pan4, pan5, pan6, 
                          panel.names = c("Full Sample (Unified)", "Full Sample (Divided)", "Bureaucrats (Unified)", "Bureaucrats (Divided)",
                                          "Staff (Unified)", "Staff (Divided)"))

StaffPanels <- star_notes_tex(StaffPanels, note.type = "threeparttable",
                              note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. 
                              Future-treated firms and treatment-switchers are never included. In all models, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, 
                              logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

star_tex_write(StaffPanels, 
               file = "../tables/TableD10.tex")

# staff salary ------------------------------------------------------------

load("../Data/staff_firms_info.RData")
# create treatment var of staff salary quartile

staff_salary <- staff_info_panel %>% 
  select(reg_lobby_core_id, period, mean_salary, cmte_staff, personal_staff) %>% 
  filter(!is.na(mean_salary)) %>% 
  ungroup %>% 
  mutate(salary_tile = ntile(mean_salary, 5)) %>% 
  mutate(cmte_staff = ifelse(cmte_staff > 0, 1, 0),
         personal_staff = ifelse(personal_staff >0, 1, 0))

combined_panel <- left_join(combined_panel, staff_salary)
combined_panel <- combined_panel %>% 
  ungroup %>% 
  mutate_at(vars(cmte_staff:salary_tile), ~replace_na(., 0))

## cmte staff

combined_panel <- combined_panel %>%
  mutate(period = as.integer(period)) %>%
  as.data.frame()

cmte_mod <- pm_wrap(lags = 6, dataset = combined_panel,
                treat = "cmte_staff", refine = F, DV = "log_rev", seed = 888)

############
# compile and export tables for LaTeX

# twoway FE
mod <- felm(log_rev ~ cmte_staff
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 
summary(mod)


############
# compile and export tables for LaTeX

# twoway FE
mod <- felm(log_rev ~ personal_staff
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 
summary(mod)

# Panel A: committee staff
cmte_panel <- stargazer(mod, mod, mod, mod, mod, 
          coef =  cmte_mod$res[,1], se = cmte_mod$res[,2],
          covariate.labels = c("Lobbyist Becomes Committee Staffer"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue", 
          title = c("Lobbyists as Committee Staff"), 
          label = c("cmte_staff"),
          add.lines = list(c("Treatment Events", cmte_mod$n_treat, cmte_mod$n_treat, cmte_mod$n_treat,
                             cmte_mod$n_treat, cmte_mod$n_treat),
                           c("Control Firms", cmte_mod$n_control, cmte_mod$n_control,
                             cmte_mod$n_control, cmte_mod$n_control, cmte_mod$n_control),
                           c("Unique Controls", cmte_mod$unique_control_txt, cmte_mod$unique_control_txt,
                             cmte_mod$unique_control_txt, cmte_mod$unique_control_txt, cmte_mod$unique_control_txt)),
          header = F)

## personal staff
pers_mod <- pm_wrap(lags = 6, dataset = combined_panel,
                treat = "personal_staff", refine = F, DV = "log_rev", seed = 888)

# panel B: personal staff
pers_panel <- stargazer(mod, mod, mod, mod, mod, 
          coef =  pers_mod$res[,1], se = pers_mod$res[,2],
          covariate.labels = c("Lobbyist Becomes Personal Office Staffer"),
          column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
          omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
          dep.var.labels = "ln Revenue", 
          title = c("Lobbyists as Personal Office Staff"), 
          label = c("personal"),
          add.lines = list(c("Treatment Events", pers_mod$n_treat, pers_mod$n_treat, pers_mod$n_treat,
                             pers_mod$n_treat, pers_mod$n_treat),
                           c("Control Firms", pers_mod$n_control, pers_mod$n_control,
                             pers_mod$n_control, pers_mod$n_control, pers_mod$n_control),
                           c("Unique Controls", pers_mod$unique_control_txt, pers_mod$unique_control_txt,
                             pers_mod$unique_control_txt, pers_mod$unique_control_txt, pers_mod$unique_control_txt)),
          header = F)

# put tables together as panels
CmtePersPanels <- star_panel(cmte_panel, pers_panel, 
                          panel.names = c("Lobbyist as Committee Staff", "Lobbyist as Personal Office Staff"))

# add a note
CmtePersPanels <- star_notes_tex(CmtePersPanels, note.type = "threeparttable",
                              note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(CmtePersPanels, 
               file = "../tables/TableD4.tex")


#--------------------------------------------
# Compare two way fixed effects to PanelMatch

twfe <- felm(log(revenue) ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 
summary(twfe)

# event study estimates with twfe
combined_panel2 <- combined_panel %>%
  filter(treat == 1) %>%
  group_by(reg_lobby_core_id) %>%
  mutate(firstocc = min(period)) %>%
  filter(duplicated(reg_lobby_core_id) == F)

combined_panel <- left_join(combined_panel, combined_panel2[, c(1,31)], by = "reg_lobby_core_id")


combined_panel$event_time <- combined_panel$period - combined_panel$firstocc

mod <- felm(log(revenue) ~ relevel(factor(event_time), ref = "-1" )
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 
summary(mod)

fe_res <- data.frame(coef(mod),
                       sqrt(diag(vcov(mod))),
                       et = c(-40:-2,0:38))
fe_res <- fe_res %>%
  filter(coef.mod. != "NaN" & et %in% 0:4) 

names(fe_res)[1:2]<-c("pe", "se")


# baseline model
comb <- pm_wrap(lags = 6, dataset = combined_panel,
                treat = "treat", refine = F, DV = "log_rev", seed = 888)

comb$res$et <- 0:4

# plot it
p_fe<-ggplot(fe_res, aes(x = et, y = pe)) +
  geom_ribbon(aes(ymin = pe -1.43*se,
                  ymax = pe + 1.43*se), alpha = .2) +
  geom_point() +
  geom_line() +
  geom_ribbon(data = comb$res,
              aes(x = et,
                  ymin = estimate-1.43*std.error,
                  ymax = estimate + 1.43*std.error), alpha = .2, fill = "blue",
              inherit.aes = F) +
  geom_point(data = comb$res, aes(x = et, y = estimate), colour = "blue",
             inherit.aes = F) +
  geom_line(data = comb$res, aes(x = et, y = estimate), colour = "blue",
            inherit.aes = F) +  
  theme_classic() +
  geom_hline(yintercept = 0, lty = 3) +
  labs(x = "Time Since Lobbyist Become Official",
       y = "Twoway Fixed Effects Estimate") +
  annotate(geom = "text", x = 3, y = 0.5,
           label = "Matched DiD")+
  annotate(geom = "text", x = 3, y = -0.65,
           label = "TWFE")

p_fe

ggsave(filename = "../images/FigureB5.pdf",
       p_fe,
       width = 7.5, height = 5.5)


#----------------------------------------
# pre/post-HLOGA

###########
# estimate models

preHLOGA <- filter(combined_panel, period < 16)
postHLOGA <- filter(combined_panel, period > 15)

res_pre <- pm_wrap(lags = 6, dataset = preHLOGA,
                treat = "treat", refine = F, DV = "log_rev", seed = 888)

#res_pre$res


res_post <- pm_wrap(lags = 6, dataset = postHLOGA,
                   treat = "treat", refine = F, DV = "log_rev", seed = 888)

#res_post$res

mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = postHLOGA) 

pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  res_post$res[,1], se = res_post$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Lobby Firm Revenue"), 
                  label = c("hloga"),
                  # add.lines = list(c("Treatment Events", res_post$n_treat, res_post$n_treat, res_post$n_treat,
                  #                    res_post$n_treat, res_post$n_treat),
                  #                  c("Control Firms", res_post$n_control,res_post$n_control,
                  #                    res_post$n_control, res_post$n_control, res_post$n_control),
                  #                  c("Unique Controls", res_post$n_unique_control, res_post$n_unique_control,
                  #                    res_post$n_unique_control, res_post$n_unique_control, res_post$n_unique_control)),
                  header = F)

post_add_lines <- c("Treatment Events & 64 & 64 & 64 & 64 & 64 \\\\",
                    "Control Firms & 55,025 & 55,025 & 55,025 & 55,025 & 55,025 \\\\",
                    "Unique Controls & 1,659 & 1,659 & 1,659 & 1,659 & 1,659 \\\\") 

#Panel B
mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = preHLOGA) 

pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  res_pre$res[,1], se = res_pre$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Effects Before and After the Passage of HLOGA"), 
                  label = c("hloga"),
                  # add.lines = list(c("Treatment Events", res_pre$n_treat, res_pre$n_treat, res_pre$n_treat,
                  #                    res_pre$n_treat, res_pre$n_treat),
                  #                  c("Control Firms", res_pre$n_control, res_pre$n_control,
                  #                    res_pre$n_control, res_pre$n_control, res_pre$n_control),
                  #                  c("Unique Controls", res_pre$n_unique_control, res_pre$n_unique_control,
                  #                    res_pre$n_unique_control, res_pre$n_unique_control, res_pre$n_unique_control)),
                  header = F)


pre_add_lines <-c("Treatment Events & 45 & 45 & 45 & 45 & 45 \\\\",                                                             
                  "Control Firms & 22,203 & 22,203 & 22,203 & 22,203 & 22,203 \\\\",                                                  
                  "Unique Controls & 973 & 973 & 973 & 973 & 973 \\\\" )

# put combined table together
hlogaPanels <- star_panel(pan1, pan2, panel.names = c("Before HLOGA", "After HLOGA"))

# add a note
hlogaPanels <- star_notes_tex(hlogaPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. In Panel B, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

hlogaPanels <- star_insert_row(hlogaPanels,
                               string = post_add_lines,
                               insert.after = 22)

hlogaPanels <- star_insert_row(hlogaPanels,
                               string = pre_add_lines,
                               insert.after = 18)

# export
star_tex_write(hlogaPanels, 
               file = "../tables/TableD7.tex")


#--------------------------------
# Lobbyist leaving government service



combined_panel2 <- combined_panel %>%
  group_by(reg_lobby_core_id) %>%
  #arrange(desc(period)) %>%
  mutate(lead_treat = dplyr::lag(treat, 1, order_by = period),
         last_treat = case_when(treat == 0 & lead_treat == 1 ~ 1,
                                TRUE ~ 0),
         lead_last_occ = dplyr::lag(last_treat, 1, order_by = period),
         lead_last_occ2 = dplyr::lag(last_treat, 2, order_by = period),
         lead_last_occ3 = dplyr::lag(last_treat, 3, order_by = period),
         lead_last_occ4 = dplyr::lag(last_treat, 4, order_by = period),
         last_occ_2year = case_when(last_treat == 1 ~ 1,
                                    lead_last_occ == 1 ~ 1,
                                    lead_last_occ2 == 1 ~ 1,
                                    lead_last_occ3 == 1 ~ 1, 
                                    lead_last_occ4 == 1 ~ 1,
                                    TRUE ~ 0))


combined_panel2 <- as.data.frame(combined_panel2)


pm_leave <- pm_wrap(lags = 6, dataset = combined_panel2,
                    treat = "last_occ_2year", refine = F, DV = "log_rev", seed = 888)

pm_leave_refine <- pm_wrap(lags = 6, dataset = combined_panel2,
                           treat = "last_occ_2year", refine = T, DV = "log_rev", seed = 888)

# Panel A
pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  pm_leave$res[,1], se = pm_leave$res[,2],
                  covariate.labels = c("Lobby Firm Loses Connection"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Former Lobbyist Leaving Government Job and Lobby Firm Revenue"), 
                  label = c("lose_connex"),
                  add.lines = list(c("Treatment Events", pm_leave$n_treat, pm_leave$n_treat, pm_leave$n_treat,
                                     pm_leave$n_treat, pm_leave$n_treat),
                                   c("Control Firms", pm_leave$n_control, pm_leave$n_control,
                                     pm_leave$n_control, pm_leave$n_control, pm_leave$n_control),
                                   c("Unique Controls", pm_leave$n_unique_control, pm_leave$n_unique_control,
                                     pm_leave$n_unique_control, pm_leave$n_unique_control, pm_leave$n_unique_control)),
                  header = F)

#Panel B
pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  pm_leave_refine$res[,1], se = pm_leave_refine$res[,2],
                  covariate.labels = c("Lobby Firm Loses Connection"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Former Lobbyist Leaving Government Job and Lobby Firm Revenue"), 
                  label = c("lose_connex"),
                  add.lines = list(c("Treatment Events", pm_leave_refine$n_treat, pm_leave_refine$n_treat, pm_leave_refine$n_treat,
                                     pm_leave_refine$n_treat, pm_leave_refine$n_treat),
                                   c("Control Firms", pm_leave_refine$n_control, pm_leave_refine$n_control,
                                     pm_leave_refine$n_control, pm_leave_refine$n_control, pm_leave_refine$n_control),
                                   c("Unique Controls", pm_leave_refine$n_unique_control, pm_leave_refine$n_unique_control,
                                     pm_leave_refine$n_unique_control, pm_leave_refine$n_unique_control, pm_leave_refine$n_unique_control)),
                  header = F)

# put combined table together
BothPanels <- star_panel(pan1, pan2, panel.names = c("Unadjusted Estimates", "Adjusted Estimates"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. In Panel B, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")


# export
star_tex_write(BothPanels, 
               file = "../tables/TableD8.tex")


#---------------------------------
# Effects conditional on partisanship
load("../Data/staff_party_main_results.RData")

staff_firms_party_panel <- select(staff_firms_party_panel, reg_lobby_core_id, period, any_dem)

staff_exp_firm <- left_join(staff_exp_firm, staff_firms_party_panel, 
                            by = c("reg_lobby_core_id", "period"))

staff_exp_firm$any_dem <- ifelse(is.na(staff_exp_firm$any_dem) == T, -1, 
                                 staff_exp_firm$any_dem)

d_vs_control <- filter(staff_exp_firm, any_dem == 1 | any_dem == -1)
r_vs_control <- filter(staff_exp_firm, any_dem == 0 | any_dem == -1)
d_vs_control$period <- as.integer(d_vs_control$period)

dem_res <- pm_wrap(lags = 6, dataset = d_vs_control, treat = "staff_exp2", 
                   refine = F,
                   DV = "log_rev", seed = 888)
dem_res$res

r_vs_control$period <- as.integer(r_vs_control$period)
oth_res <- pm_wrap(lags = 6, dataset = r_vs_control, treat = "staff_exp2", 
                   refine = F,
                   DV = "log_rev", seed = 888)

oth_res$res

# Panel A
pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  dem_res$res[,1], se = dem_res$res[,2],
                  covariate.labels = c("Lobby Firm Gains Connctions"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Effects Conditional on Partisanship of Connection"), 
                  label = c("dem"),
                  # add.lines = list(c("Treatment Events", dem_res$n_treat, dem_res$n_treat, dem_res$n_treat,
                  #                    dem_res$n_treat, dem_res$n_treat),
                  #                  c("Control Firms", dem_res$n_control, dem_res$n_control,
                  #                    dem_res$n_control, dem_res$n_control, dem_res$n_control),
                  #                  c("Unique Controls", dem_res$n_unique_control, dem_res$n_unique_control,
                  #                    dem_res$n_unique_control, dem_res$n_unique_control, dem_res$n_unique_control)),
                  header = F)

dem_info <- c("Treatment Events & 35 & 35 & 35 & 35 & 35 \\\\ ", 
              "Control Firms & 26,317 & 26,317 & 26,317 & 26,317 & 26,317 \\\\ ",
              "Unique Controls & 2,192 & 2,192 & 2,192 & 2,192 & 2,192 \\\\ ")

#Panel B
pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  oth_res$res[,1], se = oth_res$res[,2],
                  covariate.labels = c("Lobby Firm gains Connection"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Effects Conditional on Partisanship of Connection"), 
                  label = c("rep"),
                  # add.lines = list(c("Treatment Events", oth_res$n_treat, oth_res$n_treat, oth_res$n_treat,
                  #                    oth_res$n_treat, oth_res$n_treat),
                  #                  c("Control Firms", oth_res$n_control, oth_res$n_control,
                  #                    oth_res$n_control, oth_res$n_control, oth_res$n_control),
                  #                  c("Unique Controls", oth_res$n_unique_control, oth_res$n_unique_control,
                  #                    oth_res$n_unique_control, oth_res$n_unique_control, oth_res$n_unique_control)),
                  header = F)

R_info <- c("Treatment Events & 75 & 75 & 75 & 75 & 75 \\\\ ",
            "Control Firms & 54,857 & 54,857 & 54,857 & 54,857 & 54,857 \\\\ ",
            "Unique Controls & 2,234 & 2,234 & 2,234 & 2,234 & 2,234 \\\\ ")

# put combined table together
BothPanels <- star_panel(pan1, pan2, panel.names = c("Democrats", "Republicans and Cmte Staff"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. Weighted bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

BothPanels <- star_insert_row(BothPanels,
                               string = dem_info,
                               insert.after = 18)

BothPanels <- star_insert_row(BothPanels,
                               string = R_info,
                               insert.after = 25)

# export
star_tex_write(BothPanels, 
               file = "../tables/TableD9.tex")


#-----------------------------
# lame duck analysis

# create treatment
# combined_panel <- combined_panel %>%
#   group_by(reg_lobby_core_id) %>%
#   summarise(firstocc = min(period, na.rm=T)) %>%
#   right_join(combined_panel, by = "reg_lobby_core_id")

# not in operator
'%!in%' <- function(x,y)!('%in%'(x,y))

combined_panel <- combined_panel %>%
  mutate(lame_duck_treat = case_when(firstocc %!in% 
                                       c(1:4, 15:18,
                                               31:34, 39:42) ~ 0,
                                     TRUE ~ treat)) %>%
  as.data.frame()

# estimate
lame_res1 <- pm_wrap(lags = 6, dataset = combined_panel, 
                          treat = "lame_duck_treat", refine = F,
                          DV = "log_rev", seed = 888)
lame_res1$res

# with adjustments
lame_res2 <- pm_wrap(lags = 6, dataset = combined_panel, 
                          treat = "lame_duck_treat", refine = T,
                          DV = "log_rev", seed = 888)

lame_res2$res

#make tables
pan_lame1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  lame_res1$res[,1], se = lame_res1$res[,2],
                  covariate.labels = c("Lobby Firm Gains Connctions"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Effects of Connections Made During Lame Duck Periods"), 
                  label = c("lame"),
                   add.lines = list(c("Treatment Events", lame_res1$n_treat, lame_res1$n_treat, lame_res1$n_treat,
                                      lame_res1$n_treat, lame_res1$n_treat),
                                    c("Control Firms", lame_res1$n_control, lame_res1$n_control,
                                      lame_res1$n_control, lame_res1$n_control, lame_res1$n_control),
                                    c("Unique Controls", lame_res1$n_unique_control, lame_res1$n_unique_control,
                                      lame_res1$n_unique_control, lame_res1$n_unique_control, lame_res1$n_unique_control)),
                  header = F)

pan_lame2 <- stargazer(mod, mod, mod, mod, mod, 
                       coef =  lame_res2$res[,1], se = lame_res2$res[,2],
                       covariate.labels = c("Lobby Firm Gains Connctions"),
                       column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                       omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                       dep.var.labels = "ln Revenue", 
                       title = c("Effects of Connections Made During Lame Duck Periods"), 
                       label = c("lame"),
                       add.lines = list(c("Treatment Events", lame_res2$n_treat, lame_res2$n_treat, lame_res2$n_treat,
                                          lame_res2$n_treat, lame_res2$n_treat),
                                        c("Control Firms", lame_res2$n_control, lame_res2$n_control,
                                          lame_res2$n_control, lame_res2$n_control, lame_res2$n_control),
                                        c("Unique Controls", lame_res2$n_unique_control, lame_res2$n_unique_control,
                                          lame_res2$n_unique_control, lame_res2$n_unique_control, lame_res2$n_unique_control)),
                       header = F)


# put combined table together
LamePanels <- star_panel(pan_lame1, pan_lame2, panel.names = c("Democrats", "Republicans and Cmte Staff"))

# add a note
LamePanels <- star_notes_tex(LamePanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. Weighted bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(LamePanels, 
               file = "../tables/TableD15.tex")



#-----------------------------
# robustness

# convenience function for estimating models with different lag lengths


robust_func <- function(lags, dataset, treat,
                    DV, seed){

    comb <- PanelMatch(lag = lags, 
                       time.id = "period", unit.id = "reg_lobby_core_id", 
                       treatment = treat, 
                       refinement.method = "mahalanobis", #alternative none
                       data = dataset, 
                       match.missing = T, 
                       covs.formula = ~ I(lag(log_contracts,1:4)) + I(lag(log_lobby,1:4)) + I(lag(log_rev,1:4)),
                       #size.match = 10, 
                       qoi = "att" ,
                       outcome.var = DV,
                       lead = 0:4, #0:8 as robustness or vice versa
                       forbid.treatment.reversal = T)

  
  set.seed(seed)
  comb_results <- PanelEstimate(sets = comb, 
                                data = dataset, 
                                confidence.level = .9)
  
  pm_res <- as.data.frame(summary(comb_results)[[1]][1:5, 1:2])
  
  
  return(res = pm_res)
  
}

# combined panel

# this will take a while to run
res <- map_df(1:11, ~ robust_func(lags = ., dataset = combined_panel,
                              treat = "treat", DV = "log_rev", seed = 888))
res$n_lead <- rep(0:4, 11)
res$n_lag <- rep(1:11, each = 5)

p_rob <- res %>%
filter(n_lead == 0) %>%
ggplot(aes(x = n_lag, y = estimate)) +
  geom_point() +
  geom_errorbar(aes(ymin = estimate - 1.645*std.error,
                    ymax = estimate + 1.645*std.error),
                width = 0) +
  theme_classic() +
  geom_hline(yintercept = 0, lty = 3)+
  labs(x = NULL,
       y = "Contemporaneous ATT",
       title = "A: Lobbyists-into-Government") +
  scale_x_continuous(breaks = 1:12)

p_rob

# bureaucrats
b_res <- map_df(1:11, ~ robust_func(lags = ., dataset = prev_exp_firm,
                              treat = "prev_exp2", DV = "log_rev", seed = 888))
b_res$n_lead <- rep(0:4, 11)
b_res$n_lag <- rep(1:11, each = 5)

p_rob_b <- b_res %>%
  filter(n_lead == 0) %>%
  ggplot(aes(x = n_lag, y = estimate)) +
  geom_point() +
  geom_errorbar(aes(ymin = estimate - 1.645*std.error,
                    ymax = estimate + 1.645*std.error),
                width = 0) +
  theme_classic() +
  geom_hline(yintercept = 0, lty = 3)+
  labs(x = "Lag Length Where Treatment Histories Are The Same",
       y = "Contemporaneous ATT",
       title = "C: Lobbyists-turning-Bureaucrats") +
  scale_x_continuous(breaks = 1:12)

p_rob_b

# staff
staff_exp_firm$period <- as.integer(staff_exp_firm$period)
s_res <- map_df(1:11, ~ robust_func(lags = ., dataset = staff_exp_firm,
                                treat = "staff_exp2",  DV = "log_rev", seed = 888))

s_res$n_lead <- rep(0:4, 11)
s_res$n_lag <- rep(1:11, each = 5)

p_rob_s <- s_res %>%
  filter(n_lead == 0) %>%
  ggplot(aes(x = n_lag, y = estimate)) +
  geom_point() +
  geom_errorbar(aes(ymin = estimate - 1.645*std.error,
                    ymax = estimate + 1.645*std.error),
                width = 0) +
  theme_classic() +
  geom_hline(yintercept = 0, lty = 3)+
  labs(x = NULL,
       y = "Contemporaneous ATT",
       title = "B: Lobbyists-turning-Staffers") +
  scale_x_continuous(breaks = 1:11)

p_rob_s

##
# export plot

full_rob <- plot_grid(p_rob,
                      p_rob_s,
                      p_rob_b,
                      ncol = 1)

full_rob


 ggsave(full_rob,
        filename = "../images/FigureD8.pdf",
        width = 7, height = 15)

